package com.fare.taluo.fetl.domain.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.fetl.domain.entity.OfficChannelSpreadEntity;
import com.fare.taluo.fetl.domain.repository.IOfficChannelSpreadRepository;
import com.fare.taluo.util.DateUtils;

@Repository("officChannelSpreadRepository")
public class OfficChannelSpreadRepositoryImpl extends
		EntityFetlHibernateRepository implements IOfficChannelSpreadRepository {

	@Override
	public List<OfficChannelSpreadEntity> findByCondition(Date reportDate,
			Date flightDate, String fromCity, String toCity, String flightNo,
			String channel, String hbaseKey) {
		StringBuffer jpql =new StringBuffer("select entity from OfficChannelSpreadEntity entity where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(reportDate!=null){
			jpql.append(" and entity.reportDate =? ");
			conditionVals.add(reportDate);
		}
		if(flightDate !=null){
			jpql.append(" and entity.flightDate =? ");
			conditionVals.add(flightDate);
		}
		if(null!=fromCity && !"".equals(fromCity)){
			jpql.append(" and entity.fromCity =? ");
			conditionVals.add(fromCity);
		}
		if(null!=toCity && !"".equals(toCity)){
			jpql.append(" and entity.toCity =? ");
			conditionVals.add(toCity);
		}
		if(null!=flightNo && !"".equals(flightNo)){
			jpql.append(" and entity.flightNo =? ");
			conditionVals.add(flightNo);
		}
		if(null!=hbaseKey && !"".equals(hbaseKey)){
			jpql.append(" and entity.hbaseKey =? ");
			conditionVals.add(hbaseKey);
		}
		if(null!=channel && !"".equals(channel)){
			jpql.append(" and entity.channel =? ");
			conditionVals.add(channel);
		}
	
		jpql.append(" order by entity.grabTime desc ");
		Session session =getSessionFactory1().openSession();
		
		List<OfficChannelSpreadEntity> list =null;
		try {
			Query query = session.createQuery(jpql.toString());
			for (int i = 0; i < conditionVals.size(); i++) {
				query = query.setParameter(i, conditionVals.get(i));
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		return list;
	}

	@Override
	public List<OfficChannelSpreadEntity> findBySaven(Date startDay,
			Date endDay, String fromCity, String toCity, String hbaseKey) {
		StringBuffer jpql =new StringBuffer("select entity from OfficChannelSpreadEntity entity where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(startDay!=null){
			jpql.append(" and entity.reportDate >=? ");
			conditionVals.add(startDay);
		}
		if(endDay !=null){
			jpql.append(" and entity.reportDate  <=? ");
			conditionVals.add(endDay);
		}
		if(null!=fromCity && !"".equals(fromCity)){
			jpql.append(" and entity.fromCity =? ");
			conditionVals.add(fromCity);
		}
		if(null!=toCity && !"".equals(toCity)){
			jpql.append(" and entity.toCity =? ");
			conditionVals.add(toCity);
		}
		
		if(null!=hbaseKey && !"".equals(hbaseKey)){
			jpql.append(" and entity.hbaseKey =? ");
			conditionVals.add(hbaseKey);
		}
		
	
		jpql.append(" order by entity.grabTime desc ");
		Session session =getSessionFactory1().openSession();
		
		List<OfficChannelSpreadEntity> list =null;
		try {
			Query query = session.createQuery(jpql.toString());
			for (int i = 0; i < conditionVals.size(); i++) {
				query = query.setParameter(i, conditionVals.get(i));
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		return list;
	}

}
